

#import "APProtocolControl.h"

@implementation APProtocolControl

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        return [self initWithColor:LYRGBAColor(255, 255, 255, 0.6) highlightColor:UIColor.whiteColor];
    }
    return self;
}

- (instancetype)initWithColor:(UIColor *)normalColor highlightColor:(UIColor *)highlightColor{
    if (self = [super initWithFrame:UIScreen.mainScreen.bounds]) {
        WS(ws);
        
        _checkIV = [[UIImageView alloc] init];
        [_checkIV setImage:[UIImage imageNamed:@"login_check_n"]];
        [_checkIV setHighlightedImage:[UIImage imageNamed:@"login_check_h"]];
        [self addSubview:_checkIV];
        
        NSString *memo = @"By using our App you agree with our  Term of Use  and Privacy Policy.";
        NSString *relief = @"Term of Use";

        NSString *privacy = @"Privacy Policy";
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:memo];
        NSRange contentRange = [memo rangeOfString:relief];
        NSRange privacyRange = [memo rangeOfString:privacy];
        [attrString addAttributes:@{NSForegroundColorAttributeName:normalColor} range:NSMakeRange(0, memo.length)];
        [attrString addAttributes:@{NSFontAttributeName :[LYFont fontWithSize:12 fontName:LYFontNamepfr], NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName : highlightColor, NSForegroundColorAttributeName : highlightColor} range:contentRange];
        [attrString addAttributes:@{NSFontAttributeName :[LYFont fontWithSize:12 fontName:LYFontNamepfr], NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName : highlightColor, NSForegroundColorAttributeName : highlightColor} range:privacyRange];
        _reliefLabel = [[YYLabel alloc] init];
        _reliefLabel.numberOfLines = 2;
        _reliefLabel.textAlignment = NSTextAlignmentCenter;
        _reliefLabel.textColor = LYRGBAColor(255, 255, 255, 0.6);
        _reliefLabel.attributedText = attrString;
        [_reliefLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
        [_reliefLabel setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];

        [_reliefLabel setTextTapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            if (NSLocationInRange(range.location, contentRange)) {
                if (ws.delegate && [ws.delegate respondsToSelector:@selector(termofUseDidClick)]) {
                    [ws.delegate termofUseDidClick];
                }
            }else if (NSLocationInRange(range.location, privacyRange)){
                if (ws.delegate && [ws.delegate respondsToSelector:@selector(privacyPolicyDidClick)]) {
                    [ws.delegate privacyPolicyDidClick];
                }
            }else{
                if (ws.checkIV.highlighted) {
                    ws.checkIV.highlighted = NO;
                }else{
                    ws.checkIV.highlighted = YES;
                }
                if (ws.delegate && [ws.delegate respondsToSelector:@selector(checkButtonDidClick:)]) {
                    [ws.delegate checkButtonDidClick:ws.checkIV.isHighlighted];
                }
            }
        }];
        [self addSubview:_reliefLabel];
        [_reliefLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(ws).with.offset(0);
            make.centerX.equalTo(ws.mas_centerX).with.offset(7);
            make.width.mas_lessThanOrEqualTo(ScreenWidth - 120);
        }];
        
        [_checkIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(ws.reliefLabel.mas_left).with.offset(-10);
            make.centerY.equalTo(ws.reliefLabel).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(16, 16));
        }];
        
        [self addTarget:self action:@selector(checkControlDidCLick) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


- (void)setIsCheckIconVisible:(_Bool)isCheckIconVisible{
    _isCheckIconVisible = isCheckIconVisible;
    _checkIV.hidden = !isCheckIconVisible;
}

- (void)checkControlDidCLick{
    if (_checkIV.highlighted) {
        _checkIV.highlighted = NO;
    }else{
        _checkIV.highlighted = YES;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkButtonDidClick:)]) {
        [self.delegate checkButtonDidClick:self.checkIV.isHighlighted];
    }
}


- (BOOL)checkSelectStatus{
    return _checkIV.highlighted;
}

- (void)setCheckSelectedStatus:(BOOL)isSelected{
    self.checkIV.highlighted = isSelected;
}



@end
